xlv.obj.viewerLookup={
	extend:function(){
		this.extend('xlv.obj.viewer');
	},
	implement:function(){
	},
	create:function(tag){
		var div=document.createElement((tag||'div'));
		div.setAttribute('xlvobject','xlv.obj.viewerLookup');
		return div;			
	},
	properties:{
		dmName:null,
		dmPath:null,
		lookupdmName:null,
		lookupdmPath:null,
		lookupdmTextPath:null,
		lookupdmQueryPath:null
	},
	method:{
		initial:function(){
			this.$super('xlv.obj.viewer.initial');
			this.$V.lookupReady=false;
			this.$V.lookupValueForView=null;
			this.$V.defer_mvc_valueToView=null;
		},
		/**mvc imp*/
		_isModel:function(dm){
			return this.$P.dmName==dm.getName();
		},
		_isLookupModel:function(dm){
			return this.$P.lookupdmName==dm.getName();
		},
		mvc_isModel:function(dm){
			return (this._isModel(dm)||this._isLookupModel(dm));
		},
		mvc_addListener:function(){
			var dm;
			if(dm=this.mvc_getModel(this.$P.dmName))dm.addListener(this);
			if(dm=this.mvc_getModel(this.$P.lookupdmName))dm.addListener(this);
		},
		mvc_removeListener:function(){
			var dm;
			if(dm=this.mvc_getModel(this.$P.dmName))dm.removeListener(this);
			if(dm=this.mvc_getModel(this.$P.lookupdmName))dm.removeListener(this);
		},
		mvc_valueFromModel:function(dm,index){
			if(this._isModel(dm)) return dm.getValue(this.$P.dmPath);
			if(this._isLookupModel(dm))return dm.getValue(this.$P.lookupdmPath,index+1);
			return null;
		},
		mvc_valueToModel:function(value,dm,index){
			if(this._isModel(dm)) dm.setValue(value,this.$P.dmPath);
			else if(this._isLookupModel(dm))dm.setValue(value,this.$P.lookupdmPath,index+1);
		},
		mvc_valueFromModel:function(dm){
			return this._isModel(dm) ? dm.getValue(this.$P.dmPath) : null;
		},
		mvc_valueToModel:function(value,dm){
			if(this._isModel(dm)) dm.setValue(value,this.$P.dmPath);
		},
		mvc_afterModelChanged:function(dm,notify,memo){
			if(!this.mvc_isModel(dm))return;
			var v=this.$V;
			switch(notify){
				case 'setChanged' :
					if(this._isLookupModel(dm)){
						v.lookupReady=true;
						if(v.defer_mvc_valueToView) {
							v.defer_mvc_valueToView();
							v.defer_mvc_valueToView=null;
						}
						if(v.lookupValueForView!=null){
							var value=dm.map(this.$P.lookupdmPath,v.lookupValueForView,this.$P.lookupdmTextPath);
							if(value!=null){
								v.lookupValueForView=null;
								this.mvc_valueToView(null,value);
							}
						}
					}
					break;
				case 'rowChanged':
					if(this._isModel(dm))this.mvc_valueToView(this.mvc_valueFromModel(dm));		
					break;
				case 'cursorMove' :
					if(this._isModel(dm)) this.mvc_valueToView(this.mvc_valueFromModel(dm));
					break;
			}
			this.mvc_enableControl(dm);
		},
		mvc_valueFromView:function(){
			return xlv.util.getElementValue(this);
		},
		mvc_valueToView:function(value,text){
			var v=this.$V;
			if(value==null&&text!=null){
				xlv.util.setElementValue(this,text);
				return;
			}
			xlv.util.setElementValue(this,'');
			if(!this.$P.lookupdmTextPath)xlv.util.setElementValue(this,value);
			else{
				if(this.$V.lookupReady){
					var lpdm=this.mvc_getModel(this.$P.lookupdmName);
					if(lpdm && (value!=null && value.length>0)){
						text=lpdm.map(this.$P.lookupdmPath,value,this.$P.lookupdmTextPath);
						if(text!=null) xlv.util.setElementValue(this,text);
						else{
							if(this.$P.lookupdmQueryPath != null){
								v.lookupValueForView=value;
								var node=lpdm.getNode(this.$P.lookupdmQueryPath);
								node.setAttribute('operator','=');
								xlv.util.setElementValue(node,value);
								if(this.$V._query_timeout)clearTimeout(this.$V._query_timeout);
								this.$V._query_timeout=setTimeout((function(){lpdm.query();}).bind(this),200);
							}
						}
					}
				}else this.$V.defer_mvc_valueToView=(function(){this.mvc_valueToView(value);}).bind(this);
			}
		}
	}
};
